export default {
    // 参数接收
    getUrlParams: function (param, k, p) {
        if (typeof param != 'string') return {};
        k = k ? k : '&'; //整体参数分隔符
        p = p ? p : '='; //单个参数分隔符
        var value = {};
        if (param.indexOf(k) !== -1) {
            param = param.split(k);
            for (var val in param) {
                if (param[val].indexOf(p) !== -1) {
                    var item = param[val].split(p);
                    value[item[0]] = item[1];
                }
            }
        } else if (param.indexOf(p) !== -1) {
            var item = param.split(p);
            value[item[0]] = item[1];
        } else {
            return param;
        }
        return value;
    },
    // 数字转汉字
    numberToChinese: function (num) {
        var chineseNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        var chineseUnit = ["", "十", "百", "千", "万", "亿"];
        var result = "";
        var index = 0;
        while (num > 0) {
            var digit = num % 10;
            if (digit !== 0 || index === 0) {
                result = chineseNum[digit] + chineseUnit[index] + result;
            }
            num = Math.floor(num / 10);
            index++;
        }
        return result;
    },

    numberToWeek: function (num) {
        if (typeof (num) != 'number') return
        var weekNum = ["周末", "周一", "周二", "周三", "周四", "周五", "周六"];
        return weekNum[num]
    },

    // 日期格式化
    dateFormat: function (date, format) {
        date = new Date(date)
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr((
                    "" + o[k]).length)));
            }
        }
        return format;
    },

    // 时间倒计时
    timeRemaining(timestamp, tips) {
        const currentTime = new Date().getTime();
        const targetTime = new Date(timestamp).getTime();
        const timeDiff = targetTime - currentTime;

        if (timeDiff <= 0) {
            return tips || "目标时间已过";
        }

        const seconds = Math.floor(timeDiff / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);

        if (days > 0) {
            return `${days}天${hours % 24}小时${minutes % 60}分钟${seconds % 60}秒`;
        } else if (hours > 0) {
            return `${hours}小时${minutes % 60}分钟${seconds % 60}秒`;
        } else if (minutes > 0) {
            return `${minutes}分钟${seconds % 60}秒`;
        } else {
            return `${seconds}秒`;
        }
    },

    // 对象展开 转 url参数
    objectToUrlParams(obj) {
        const params = [];

        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                let value = obj[key];
                // URL 编码属性名和属性值
                let encodedKey = encodeURIComponent(key);
                let encodedValue = encodeURIComponent(value);
                // 将属性名和属性值连接成字符串，并添加到数组中
                params.push(`${encodedKey}=${encodedValue}`);
            }
        }

        // 使用 & 符号连接所有参数
        return 'scene=' + encodeURIComponent(params.join('&'));
    },

    // 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
    getDistances: function ({lat1, lng1, lat2, lng2}) {
        let EARTH_RADIUS = 6378.137;// 地球半径
        let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
        let radLat2 = lat2 * Math.PI / 180.0;
        let a = radLat1 - radLat2;
        let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
        let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;// 输出为公里
        // return { m: s * 1000, km: Number(s.toFixed(2)) }
        // 返回公里数
        return Number(s.toFixed(2))
    }
}